<?php

use Drupal\shusy_expert\Entity\ShusyExpert;
use Drupal\shusy_expert\Entity\ShusyExpertType;

function shusy_expert_shusy_expert_entity_form($form, &$form_state, ShusyExpert $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_expert') . '/shusy_expert.forms.inc';
  $form_state[ShusyExpert::class] = $e;

  $types = shusy_expert_type_load_multiple(FALSE);
  $options = [];
  foreach ($types as $type) {
    if ($type instanceof ShusyExpertType) {
      $options[$type->id] = $type->name;
    }
  }
  $form['expert_type_id'] = [
    '#default_value' => $e->expert_type_id,
    '#type' => 'select',
    '#title' => t('分类'),
    '#options' => $options,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('姓名'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['avatar'] = [
    '#type' => 'managed_file',
    '#title' => t('头像'),
    '#required' => TRUE,
    '#default_value' => $e->avatar,
    '#upload_location' => 'public://tmp/',
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => t('头衔'),
    '#required' => TRUE,
    '#default_value' => $e->title,
  ];
  $form['phone'] = [
    '#type' => 'textfield',
    '#title' => t('咨询电话'),
    '#required' => TRUE,
    '#default_value' => $e->phone,
  ];
  $form['experience'] = [
    '#type' => 'textfield',
    '#title' => t('经验年限'),
    '#required' => TRUE,
    '#default_value' => $e->experience,
  ];
  $form['summary'] = [
    '#type' => 'textarea',
    '#title' => t('摘要介绍'),
    '#default_value' => $e->summary,
  ];
  $form['authentication'] = [
    '#type' => 'select',
    '#title' => t('认证'),
    '#options' => $e->getAuthenticationOptions(),
    '#default_value' => $e->authentication,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_expert_shusy_expert_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpert::class];
  if ($e instanceof ShusyExpert) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_expert_shusy_expert_delete_form($form, &$form_state, ShusyExpert $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_expert') . '/shusy_expert.forms.inc';
  $form_state[ShusyExpert::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_expert/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_expert_shusy_expert_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpert::class];
  if ($e instanceof ShusyExpert) {
    $e->delete();
  }
}

function shusy_expert_shusy_expert_type_entity_form($form, &$form_state, ShusyExpertType $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_expert') . '/shusy_expert.forms.inc';
  $form_state[ShusyExpertType::class] = $e;

  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('名称'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['sort'] = [
    '#type' => 'textfield',
    '#title' => t('排序'),
    '#required' => TRUE,
    '#default_value' => $e->sort,
    '#element_validate' => ['element_validate_integer'],
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_expert_shusy_expert_type_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpertType::class];
  if ($e instanceof ShusyExpertType) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_expert_shusy_expert_type_delete_form($form, &$form_state, ShusyExpertType $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_expert') . '/shusy_expert.forms.inc';
  $form_state[ShusyExpertType::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_expert_type/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_expert_shusy_expert_type_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyExpertType::class];
  if ($e instanceof ShusyExpertType) {
    $e->delete();
  }
}

